package com.ggs.starbug.consumer.dead;

import com.ggs.starbug.entity.User;
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;

import java.io.IOException;
import java.util.Map;

import static com.ggs.starbug.config.rabbitmq.RabbitMQConstants.*;

/**
 * @Author lianghaohui
 * @Date 2021/12/29 14:27
 * @Description
 */
@Slf4j
@Configuration
public class DeadLetterConsumer {

    @Autowired
    private Gson gson;

    @RabbitHandler
    @RabbitListener(queues = DEAD_LETTER_QUEUE, ackMode = "MANUAL")
    public void deadLetterConsumer(@Payload User user, @Headers Map<String, Object> headers, Channel channel, Message message) throws IOException {
        log.info("进入死信队列...");
        log.info("数据:{}", gson.toJson(user));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
